package com.inspur.icity.bus.base.core.util;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.inspur.icity.bus.base.core.geo.WgsGcjConverter;
import com.inspur.icity.bus.base.core.geo.coords.ICoordinates;

import java.io.IOException;
import java.util.Map;

public class OnTheEarthMapJsonSerializer extends JsonSerializer<Map> {
    ObjectMapper mapper;
    public OnTheEarthMapJsonSerializer(){
        mapper = new ObjectMapper();
    }

    public OnTheEarthMapJsonSerializer(ObjectMapper mapper){
        this.mapper = mapper;
    }

    @Override
    public void serialize(Map onTheEarth, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
        if (onTheEarth.get("onTheEarth") != null && onTheEarth.get("lng")  != null && onTheEarth.get("lat")  != null){
            ICoordinates point = WgsGcjConverter.wgs84ToGcj02(Double.valueOf(onTheEarth.get("lat").toString()), Double.valueOf(onTheEarth.get("lng").toString()));
            onTheEarth.put("lng",point.getLng());
            onTheEarth.put("lat",point.getLat());
            onTheEarth.remove("onTheEarth");
        }
        mapper.writeValue(jsonGenerator, onTheEarth);
    }
}
